package ahu.zdw.com;

import java.util.Arrays;

/**
 * Author: Zhang Dongwei
 * Date: 2022/3/12 20:22
 */
public class _27_removeElement {

    public static void main(String[] args) {
        Solution solution = new _27_removeElement().new Solution();
        int[] nums = {0,1,2,2,3,0,4,2};
        int a = solution.removeElement(nums, 2);
        System.out.println(a);
    }

//    自己写的，占内存比较多，但是运行较快
    class Solution {
        public int removeElement(int[] nums, int val) {
            int j=0;
            for(int i=0; i<nums.length; i++){
                if(nums[i] == val){
                    j += 1;
                    continue;
                }
                nums[i-j] = nums[i];
            }
//            System.out.println(Arrays.toString(nums));
            return nums.length-j;
        }
//      官网给的，但是和自己写的差不多
    class Solution1 {
        public int removeElement(int[] nums, int val) {
            int n = nums.length;
            int left = 0;
            for (int right = 0; right < n; right++) {
                if (nums[right] != val) {
                    nums[left] = nums[right];
                    left++;
                }
            }
            return left;
        }
    }
    }
}
